#include "qs_config.h"
#include "qs_log.h"
#include "qs_action.h"
#include "qs_sched.h"
#include "qs_settings.h"
#include "qs_key.h"
#include "qs_pwr.h"
#include "qs_led.h"
#include "qs_rf_protocol.h"
#include "qs_tft_protocol.h"
#include "qs_usart.h"
#include "qs_flash.h"
#define Aging_Test
static uint8_t ack_count = 0;
static qs_rf_protocol_t m_tx_frame;
static int8_t flash_active = 0;
static uint16_t page_step = 0;
static uint8_t launch_flag;

static uint8_t ack_timeout_timer = 0;
static int8_t disp_active = 0;
static GPIO_TypeDef * const m_buttons_port_list[] = {QS_KEY1_PORT,  QS_KEY2_PORT, QS_KEY3_PORT,  QS_KEY4_PORT,  QS_KEY6_PORT, QS_KEY7_PORT,  QS_KEY8_PORT};
static uint32_t const m_buttons_pin_list[] = {QS_KEY1_PIN, QS_KEY2_PIN, QS_KEY3_PIN, QS_KEY4_PIN, QS_KEY6_PIN, QS_KEY7_PIN, QS_KEY8_PIN};
//static uint16_t time[] = {1800,3600,5400,7200,9000,10800,12600,14400,16200,18000,19800,21600,23400,25200,27000,28800};
static uint8_t goto_sleep_timeout_timer = 15;
static uint8_t TX_Interval = 30;
static uint8_t last_goto_sleep_timeout_timer;
static uint8_t LightUp_Flag;
static uint8_t first_key;
static uint32_t UID ;
static uint32_t RC_UID;
static bool alreadysend_flag;
static uint8_t CB_Workstatus;

static uint32_t get_simplified_uid(void) {
    uint32_t uid2 = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
    uint32_t simplified_uid = uid2;
    return simplified_uid;
}


static int8_t temp_real_get(void)
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    return p_settings_job->temp0 > p_settings_job->temp1 ? p_settings_job->temp0:p_settings_job->temp1;
}

uint8_t qs_action_humid_threshold_get(void)
{
    qs_settings_job_t* p_settings_sens = qs_settings_job_get();
    return p_settings_sens->humid0 > p_settings_sens->humid1 ? p_settings_sens->humid0:p_settings_sens->humid1;
}

static int8_t action_page0_idle_handle(uint8_t timer)
{
    switch(page_step) {
    case 0:
      qs_tft_protocol_data_send(0x10, 0x7001, 0);
      break;
      
    case 1:
        QS_LOG_INFO("page 0\r\n");
        qs_tft_protocol_data_send(0x10, 0x7000, 0x0000);
        break;
    default:;
    }
    page_step ++;
    return page_step  <= 1 ? -1:0;
}

static int8_t action_page5_auto_handle(uint8_t timer)            //除湿
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
      qs_tft_protocol_data_send(0x10, 0x7001, 63);
      break;
    case 1:
      qs_tft_protocol_temp_data_send(0x10, 0x1400, temp_real_get());
      break; 
    case 2:
      qs_tft_protocol_data_send(0x10, 0x1410, qs_action_humid_threshold_get());
      break;  
    case 3:
        qs_tft_protocol_data_send(0x10, 0x7000, 0x0005);
        break;
    default:;
    }
    page_step ++;
    return page_step  <= 4 ? -1:0;
}
static int8_t action_page6_auto_handle(uint8_t timer)           //生理
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
      qs_tft_protocol_data_send(0x10, 0x7001, 63);
      break;
    case 1:
      qs_tft_protocol_timer_data_send(0x10, 0x1510, timer);
      break; 
    case 2:
      qs_tft_protocol_temp_data_send(0x10, 0x1500, temp_real_get());
      break; 
    case 3:
      QS_LOG_INFO("shengli\r\n");
      qs_tft_protocol_data_send(0x10, 0x7000, 0x0006);
      break;
    default:;
    }
    page_step ++;
    return page_step  <= 3 ? -1:0;
}
static int8_t action_page7_auto_handle(uint8_t timer)                      //除螨模式
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
      qs_tft_protocol_data_send(0x10, 0x7001, 63);
      break;
    case 1:
      qs_tft_protocol_timer_data_send(0x10, 0x1610, timer);
      break;
    case 2:
      qs_tft_protocol_temp_data_send(0x10, 0x1600, temp_real_get());
      break;  
    case 3:
      qs_tft_protocol_data_send(0x10, 0x7000, 0x0007);
      break;
    default:;
    }
    page_step ++;
    return page_step  <= 3 ? -1:0;
}
static int8_t action_page4_manual_fan_handle(uint8_t timer)                 //风速设置
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
      qs_tft_protocol_data_send(0x10, 0x7001, 63);
      break; 
    case 1:
      qs_tft_protocol_data_send(0x10, 0x1330, (p_settings_job->fan_level) - 1);
      break;
    case 2:
      qs_tft_protocol_timer_data_send(0x10, 0x1320, timer);
      break; 
    case 3:
      qs_tft_protocol_temp_data_send(0x10, 0x1300, temp_real_get());
      break;    
    case 4:
      qs_tft_protocol_data_send(0x10, 0x7000, 0x0004);
      break;
    default:;
    }
    page_step ++;
    return page_step  <= 4 ? -1:0;
}
static int8_t action_page3_manual_heat_handle(uint8_t timer)                 //加热画面
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
        qs_tft_protocol_data_send(0x10, 0x7001, 63);
        break;     
    case 1:
        qs_tft_protocol_data_send(0x10, 0x1230, p_settings_job->heat_level + 19);
        break;
    case 2:
        qs_tft_protocol_timer_data_send(0x10, 0x1220, timer);
        break;   
    case 3:
        qs_tft_protocol_data_send(0x10, 0x1210, p_settings_job->fan_level );
        break;    
    case 4:
        qs_tft_protocol_temp_data_send(0x10, 0x1200, temp_real_get());
        break;    
    case 5:
        qs_tft_protocol_data_send(0x10, 0x7000, 0x0003);
        break;
    default:;
    }
    page_step ++;
    return page_step  <= 5 ? -1:0;
}
static int8_t action_page2_auto_handle(uint8_t timer)
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
      qs_tft_protocol_data_send(0x10, 0x7001, 63);
      break;
    case 1:
       qs_tft_protocol_timer_data_send(0x10, 0x1110, timer);
      break;      
    case 2:
       qs_tft_protocol_temp_data_send(0x10, 0x1100,temp_real_get());
      break; 
    case 3:
       qs_tft_protocol_data_send(0x10, 0x0005,0);
      break;    
    case 4:
      qs_tft_protocol_data_send(0x10, 0x7000, 0x0002);
      break;
    default:;
    }
    page_step ++;
    return page_step  <= 4 ? -1:0;
}

static int8_t action_page1_idle_handle(uint8_t timer)               //开机动画
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    switch(page_step) {
    case 0:
        QS_LOG_INFO("page 1\r\n");
        qs_tft_protocol_data_send(0x10, 0x7001, 63);
        break;
    case 1:
        qs_tft_protocol_data_send(0x10, 0x7000, 0x0001);
        break;   
    case 200:
        launch_flag = 0;
        break;
    default:;
    }
    page_step ++;
    if(page_step <= 200) { return -1;}
    else { page_step = 0;
           return 1;}
}

static void on_action_disp_handle(uint8_t timer)
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    if(disp_active != 0 ||LightUp_Flag != 0) {
        if(p_settings_job->work_mode == QS_ACTION_IDLE_MODE) {///PAGE 1
            disp_active = action_page0_idle_handle(timer);                                   //低功耗   黑屏
        }  else if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && launch_flag  == 1) {///PAGE 1
            disp_active = action_page1_idle_handle(timer);                                    //进入AI界面   
         } else if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && p_settings_job->auto_mode == 1) {
            disp_active = action_page2_auto_handle(timer);                               
        } else if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && p_settings_job->auto_mode == 2) {
            disp_active = action_page3_manual_heat_handle(timer);                             //加热设置界面                                
        } else if (p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && p_settings_job->auto_mode == 3) {
            disp_active = action_page4_manual_fan_handle(timer);                              //风速设置界面   
        }else if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && p_settings_job->auto_mode == 4) {
            disp_active = action_page6_auto_handle(timer);   
        } else if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && p_settings_job->auto_mode == 5) {
            disp_active = action_page5_auto_handle(timer); 
        } else if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && p_settings_job->auto_mode == 6) {
            disp_active = action_page7_auto_handle(timer);
        }
       LightUp_Flag =  disp_active;
    } 
    if(disp_active == 0) {
        page_step = 0;
    }
}

static void convert_setting_to_mem_data(qs_settings_job_t* p_settings_job)
{
    qs_mem_data_t* mem_data = qs_flash_mac_read();

    mem_data->work_mode = p_settings_job->work_mode;
    mem_data->fan_level = p_settings_job->fan_level;
    mem_data->heat_level = p_settings_job->heat_level;
    mem_data->auto_mode = p_settings_job->auto_mode;
    mem_data->timer = p_settings_job->timer;
    mem_data->mem_heat_level = p_settings_job->mem_heat_level;
    mem_data->mem_fan_level = p_settings_job->mem_fan_level;
    mem_data->uid[0] = (UID >> 24) & 0xFF;
    mem_data->uid[1] = (UID >> 16) & 0xFF;
    mem_data->uid[2] = (UID >> 8) & 0xFF;
    mem_data->uid[3] =  UID & 0xFF; 
    
    
//        QS_LOG_INFO(">> %lld mem , mode [%d,%d] fan [%d], heat [%d] timer %d\r\n",
//                get_sys_runtime_ms()/1000, mem_data->work_mode,mem_data->auto_mode, mem_data->fan_level, mem_data->heat_level, 
//                mem_data->timer);
    
    qs_flash_mac_write(mem_data);  
    
 
}

static void on_action_20ms_handle(void)                 //每20ms
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    static int8_t last_temp ;
    static uint8_t last_humid;
    uint8_t humid = qs_action_humid_threshold_get();
    int8_t temp = temp_real_get();
//    uint16_t timer = p_settings_job->timer/180 + (p_settings_job->timer%180 ? 1:0);                //获取定时时长
    
    uint8_t timer = p_settings_job->timer/1800+ (p_settings_job->timer%1800? 1:0); 
    
    static uint8_t last_timer = 0;
    static qs_settings_job_t last_job;
    uint8_t timer_active = 0;
    

    last_job.timer_key_timer = p_settings_job->timer_key_timer ;                                //参数更新
    if( (timer_active || p_settings_job->work_mode != last_job.work_mode ||  last_job.work_mode != p_settings_job->work_mode 
               || last_job.fan_level != p_settings_job->fan_level || last_job.heat_level != p_settings_job->heat_level
                 || last_timer != timer|| last_job.auto_mode != p_settings_job->auto_mode || temp!= last_temp || humid != last_humid)&& !launch_flag) {                                     //参数发生变化
        timer_active = 0;
        flash_active = 0;
        disp_active = 1;
        page_step = 0;     
    }
    if(last_job.work_mode == QS_ACTION_IDLE_MODE && p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE && !LightUp_Flag)
    {launch_flag = 1;}
    last_job.work_mode = p_settings_job->work_mode;
    last_job.fan_level = p_settings_job->fan_level;
    last_job.heat_level = p_settings_job->heat_level;
    last_job.auto_mode = p_settings_job->auto_mode;
    last_timer = timer;
    last_temp = temp;   
    last_humid = humid;
    on_action_disp_handle((uint8_t)timer);                  //演示
    
}



void convert_mem_data_to_setting(bool flag)
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();   
    qs_mem_data_t* mem_data = qs_flash_mac_read();
    if(flag)
    {
      if(mem_data->work_mode == 1) { 
      LightUp_Flag = 1;
      p_settings_job->work_mode = mem_data->work_mode;
      p_settings_job->fan_level = mem_data->fan_level;
      p_settings_job->heat_level = mem_data->heat_level;
      p_settings_job->auto_mode = mem_data->auto_mode;
      p_settings_job->timer = mem_data->timer;
      p_settings_job->mem_heat_level = mem_data->mem_heat_level;
      p_settings_job->mem_fan_level = mem_data->mem_fan_level;
      UID = (uint32_t)(mem_data->uid[0] <<24)|(uint32_t)(mem_data->uid[1] <<16)|(uint32_t)(mem_data->uid[2] <<8) | (uint32_t)(mem_data->uid[3]);
      flag = false;
      }else if(mem_data->work_mode == 0) { 
      p_settings_job->work_mode = mem_data->work_mode;
      UID = (uint32_t)(mem_data->uid[0] <<24)|(uint32_t)(mem_data->uid[1] <<16)|(uint32_t)(mem_data->uid[2] <<8) | (uint32_t)(mem_data->uid[3]);
      flag = false;
      }
    }
}

static void on_action_1s_handle(void)                      //低功耗
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    if(goto_sleep_timeout_timer > 0) {
        goto_sleep_timeout_timer --;
//        QS_LOG_INFO("sleep time %d\r\n",goto_sleep_timeout_timer);
        last_goto_sleep_timeout_timer = goto_sleep_timeout_timer;
    }
    if(goto_sleep_timeout_timer == 0) {
//       QS_LOG_INFO("power off\r\n");
//       if(p_settings_job->work_mode != QS_ACTION_IDLE_MODE && p_settings_job->auto_mode != 0)
//       {convert_setting_to_mem_data(p_settings_job);}
//        qs_pwr_ctrl_off();
    }
#ifdef Aging_Test
    if(p_settings_job->work_mode)
    {
      if(TX_Interval > 0){
        TX_Interval --;
      }
      if(TX_Interval == 0){
    
      }
    }else TX_Interval = 30;
#endif
}

const uint8_t local_mac[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};

static void on_job_handle(uint8_t keys)
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    static bool timer_initialized = false;
    if(keys == QS_KEYS_POWER) {       //按下电源键
         p_settings_job->work_mode= p_settings_job->work_mode != QS_ACTION_IDLE_MODE ? QS_ACTION_IDLE_MODE:QS_ACTION_AUTO_SET_MODE;    //工作模式若是低功耗模式 进入低功耗模式 若不是 进入手动设置模式
          if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE ) {   
            
            qs_mem_data_t* mem_data = qs_flash_mac_read();
            goto_sleep_timeout_timer = 15;
            p_settings_job->fan_level = (mem_data->work_mode == 0) ?mem_data->fan_level :2 ;
            p_settings_job->heat_level = (mem_data->work_mode == 0) ?mem_data->heat_level : 13;
            p_settings_job->auto_mode = (mem_data->work_mode == 0) ?mem_data->auto_mode : 1;
            if(mem_data->work_mode == 0){
              if(p_settings_job->auto_mode == 4){
                  p_settings_job->timer = 10800;
              }else if(p_settings_job->auto_mode == 6){
                  p_settings_job->timer = 14400;
              }else p_settings_job->timer = 28800;
            }else p_settings_job->timer = 28800;
            p_settings_job->mem_heat_level = (mem_data->work_mode == 0) ?mem_data->mem_heat_level : 1;
            p_settings_job->mem_fan_level = (mem_data->work_mode == 0) ?mem_data->mem_fan_level : 1;
        }  
          else if(p_settings_job->work_mode == QS_ACTION_IDLE_MODE ) {          //若是低功耗模式
            convert_setting_to_mem_data(p_settings_job);
            p_settings_job->work_mode = QS_ACTION_IDLE_MODE;
            p_settings_job->auto_mode = 0;                               //调整工作参数
            p_settings_job->fan_level = 0;                
            p_settings_job->heat_level = 0;
            p_settings_job->timer = 0;
            alreadysend_flag = true;
        }   
           
    } else if(keys == QS_KEYS_FAN_UP &&(p_settings_job->auto_mode == 2 ||p_settings_job->auto_mode == 3)) {                    //按下风速增加键
        if(p_settings_job->mem_fan_level> 5) {                      //记忆温度水平超过上限值
            p_settings_job->mem_fan_level = 5;
        }  
        p_settings_job->fan_level = p_settings_job->fan_level != p_settings_job->mem_fan_level ?  p_settings_job->mem_fan_level : (p_settings_job->fan_level < 5 ? p_settings_job->fan_level + 1 : 1);
        p_settings_job->mem_fan_level = p_settings_job->fan_level;
            
        if(p_settings_job->timer == 0) {
            p_settings_job->timer = 28800;                               //8h定时
        }
        p_settings_job->timer_key_timer = 0;
       
        
    } else if(keys == QS_KEYS_FAN_DOWN && (p_settings_job->auto_mode == 2 ||p_settings_job->auto_mode == 3 )) {       //按下风速减小键
        if(p_settings_job->mem_fan_level> 5) {                      //记忆温度水平超过上限值
            p_settings_job->mem_fan_level = 5;
        }  
        p_settings_job->fan_level = p_settings_job->fan_level != p_settings_job->mem_fan_level ?  p_settings_job->mem_fan_level : (p_settings_job->fan_level > 1 ? p_settings_job->fan_level - 1 : 5);
        p_settings_job->mem_fan_level = p_settings_job->fan_level;
         
        if(p_settings_job->timer == 0) {
            p_settings_job->timer = 28800;
        }
        p_settings_job->timer_key_timer = 0;
      
    } else if(keys == QS_KEYS_HEAT_UP &&  p_settings_job->auto_mode == 2) {        //按下加热键
        if(p_settings_job->mem_heat_level> 18) {                      //记忆温度水平超过上限值
            p_settings_job->mem_heat_level = 18;
        }      
        
        p_settings_job->heat_level = p_settings_job->heat_level != p_settings_job->mem_heat_level ?  p_settings_job->mem_heat_level : (p_settings_job->heat_level < 18 ? p_settings_job->heat_level + 1 : 18);
        p_settings_job->mem_heat_level = p_settings_job->heat_level;

        if(p_settings_job->timer == 0) {
            p_settings_job->timer = 28800;
        }
        p_settings_job->timer_key_timer = 0;
        p_settings_job->auto_mode = 2;
      
    } else if(keys == QS_KEYS_HEAT_DOWN &&  p_settings_job->auto_mode == 2 ) {
        if(p_settings_job->mem_heat_level> 18) {
            p_settings_job->mem_heat_level = 18;
        }
        
        p_settings_job->heat_level = p_settings_job->heat_level != p_settings_job->mem_heat_level ? p_settings_job->mem_heat_level : (p_settings_job->heat_level > 1 ? p_settings_job->heat_level - 1 : 1);
        p_settings_job->mem_heat_level = p_settings_job->heat_level;

        if(p_settings_job->timer == 0) {
            p_settings_job->timer = 28800;
        }
        p_settings_job->timer_key_timer = 0;
        p_settings_job->auto_mode = 2;
        

    } else if(keys == QS_KEYS_AUTO && p_settings_job->work_mode!= QS_ACTION_IDLE_MODE) {                       
        if(p_settings_job->mem_auto_mode> 6) {
            p_settings_job->mem_auto_mode = 6;
        }

        p_settings_job->auto_mode  = p_settings_job->work_mode  != QS_ACTION_IDLE_MODE ? (p_settings_job->auto_mode < 6 ? p_settings_job->auto_mode + 1 : 1) : p_settings_job->mem_auto_mode;
        p_settings_job->mem_auto_mode = p_settings_job->auto_mode;
        switch(p_settings_job->auto_mode) {
        default:;
        case 1://AI CARE                                  
          p_settings_job->fan_level = 2;
          p_settings_job->heat_level = 13;
          p_settings_job->timer = 28800;
          break;
        case 2://加热设置
          p_settings_job->fan_level = (p_settings_job->mem_fan_level!=0)? p_settings_job->mem_fan_level:(p_settings_job->mem_fan_level = 1);
          p_settings_job->heat_level = (p_settings_job->mem_heat_level!=0)? p_settings_job->mem_heat_level:(p_settings_job->mem_heat_level = 1);
          p_settings_job->timer = 28800;
          break;
        case 3://风速设置
//          p_settings_job->fan_level = 1;
          p_settings_job->heat_level = 0;
          p_settings_job->timer = 28800;
          break;
        case 4://生理模式
          p_settings_job->fan_level = 3;
          p_settings_job->heat_level = 17;
          p_settings_job->timer = 10800;
          break;  
        case 5://除湿模式
          p_settings_job->fan_level = 5;
          p_settings_job->heat_level = 13;
          p_settings_job->timer = 28800;
          break;  
        case 6://除螨模式                                
          p_settings_job->heat_level = 31;
          p_settings_job->fan_level = 3;
          p_settings_job->timer = 14400;
          break;  
                
          
        }
        p_settings_job->timer_key_timer = 0;
    } else if(keys == QS_KEYS_TIMER && (p_settings_job->auto_mode == 2||p_settings_job->auto_mode == 3)) {
      
        if(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE) {
         
                uint16_t timer = p_settings_job->timer/1800 + (p_settings_job->timer%1800 ? 1:0);
                timer = timer < 16 ? timer + 1 : 1;
                p_settings_job->timer = timer * 1800;
            
        }
        p_settings_job->work_mode= QS_ACTION_AUTO_SET_MODE;
    } else if(keys == QS_KEYS_PROV){
    }

}

static void on_tx_ready(void)
{
    static bool key_active = false;
    uint16_t keys = get_key_code() ;                        //获取键值
    static uint16_t last_keys = 0;
    static uint16_t keys_repeat = 0;
    static uint16_t rf_keys = 0;
    
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    
    last_keys |= first_key;
    if(last_keys != keys && keys != 0 && launch_flag!=1) {    
        keys_repeat = 0;
    }else if(last_keys == keys && keys != 0  )
        {
        if(keys_repeat < UINT16_MAX) keys_repeat ++;
//        QS_LOG_INFO("keys %x pressing for %d\n", keys,keys_repeat);            
        if(keys == QS_KEYS_AUTO && keys_repeat == 60 ) {   
                rf_keys = QS_KEYS_PROV;
        }
    }else if(last_keys != keys && keys == 0  && launch_flag!=1 && keys_repeat < 60) {
        key_active = true;                                 //检测到按键
        keys_repeat = 0;
        rf_keys = last_keys;
        QS_LOG_INFO("keys %x pressed\n", last_keys);            //打印键值
        goto_sleep_timeout_timer = 15;  
        
        on_job_handle(last_keys);
    }else rf_keys = 0;
#ifdef Aging_Test
    if(TX_Interval == 0){
      TX_Interval = 30;
      QS_LOG_INFO("Mode Changed!\r\n");
      rf_keys = QS_KEYS_AUTO;
      on_job_handle(QS_KEYS_AUTO);
      
    }
    
#endif

        memset(m_tx_frame.daten,0, sizeof(qs_rf_protocol_t));
        m_tx_frame.len = 12;
        m_tx_frame.type = 2;
        m_tx_frame.keys.value = (key_active &&(p_settings_job->work_mode == QS_ACTION_AUTO_SET_MODE))? rf_keys:0;
        if(alreadysend_flag) {m_tx_frame.keys.value = rf_keys;
            alreadysend_flag = false;
        }
        m_tx_frame.status.work_mode = p_settings_job->work_mode;
        m_tx_frame.status.auto_mode = p_settings_job->auto_mode;
        m_tx_frame.status.fan_level = p_settings_job->fan_level;
        m_tx_frame.status.heat_level = p_settings_job->heat_level;
        
        m_tx_frame.status.timer = p_settings_job->timer;  
        
        m_tx_frame.status.uid [0] = (UID >> 24) & 0xFF; // 最高字节
        m_tx_frame.status.uid [1] = (UID >> 16) & 0xFF; // 第二高字节
        m_tx_frame.status.uid [2] = (UID >> 8) & 0xFF;  // 第二低字节
        m_tx_frame.status.uid [3] = UID & 0xFF;         // 最低字节
        m_tx_frame.status.rc_uid [0] = (RC_UID >> 24) & 0xFF; // 最高字节
        m_tx_frame.status.rc_uid [1] = (RC_UID >> 16) & 0xFF; // 第二高字节
        m_tx_frame.status.rc_uid [2] = (RC_UID >> 8) & 0xFF;  // 第二低字节
        m_tx_frame.status.rc_uid [3] = RC_UID & 0xFF;         // 最低字节
        rf_protocol_tx_mode_data_send(&m_tx_frame);            //发送键值帧

        first_key = 0;
        if(rf_keys == QS_KEYS_PROV){
            QS_LOG_INFO("UID erase\r\n");
            UID = 0;
            rf_keys = 0;
            qs_flash_UID_erase();
         }
    last_keys = keys;
   
}

static void on_tx_payload_ack(qs_rf_protocol_t* p_frame)
{
//    static uint8_t count = 0;
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    ack_count ++;
    uint32_t RF_UID , RF_ID;
    switch(p_frame->type) {
    case 0x83:
      RF_UID = (uint32_t)(p_frame->status.uid[0] <<24)|(uint32_t)(p_frame->status.uid[1] <<16)|(uint32_t)(p_frame->status.uid[2] <<8) | (uint32_t)(p_frame->status.uid[3]); 
      if(RF_UID == UID){
        if(p_frame->status.work_mode == QS_ACTION_IDLE_MODE)
        {
        }else{
          p_settings_job->timer = p_frame->status.timer;
          p_settings_job->temp0 = p_frame->status.temp0;
          p_settings_job->humid0 = p_frame->status.humid0;
          p_settings_job->temp1 = p_frame->status.temp1;
          p_settings_job->humid1 = p_frame->status.humid1;
        }
      }
        break;
    case 0x84:
      break;
    case 0x85:
       RF_ID = (uint32_t)(p_frame->status.rc_uid[0] <<24)|(uint32_t)(p_frame->status.rc_uid[1] <<16)|(uint32_t)(p_frame->status.rc_uid[2] <<8) | (uint32_t)(p_frame->status.rc_uid[3]);
       if(RF_ID == RC_UID){
         UID = (uint32_t)(p_frame->status.uid[0] <<24)|(uint32_t)(p_frame->status.uid[1] <<16)|(uint32_t)(p_frame->status.uid[2] <<8) | (uint32_t)(p_frame->status.uid[3]); 
       }
      break;
    default:;
    }
     
}

void qs_first_key_process(void)
{
    qs_settings_job_t* p_settings_job = qs_settings_job_get();   
    if(p_settings_job->work_mode == 0)
    {
      if(LL_GPIO_IsInputPinSet(QS_KEY8_PORT,QS_KEY8_PIN) != 0)
      {
       first_key = 0x10;     
      }
    }
    Delay_ms(500);
    
    for(int idx = 0;idx < sizeof(m_buttons_pin_list)/sizeof(uint32_t); idx++ ) {
        while(LL_GPIO_IsInputPinSet(m_buttons_port_list[idx], m_buttons_pin_list[idx]) != 0) ;
    }
}


void qs_action_init(void)
{
    RC_UID = get_simplified_uid();
    qs_rf_protocol_tx_mode_event_handle_set(on_tx_ready, on_tx_payload_ack);
    qs_rf_protocol_init(RF_TX_MODE); 
    qs_tft_protocol_init();
    qs_key_init();
    qs_pwr_init();
    qs_on_action_20ms_handle_set(on_action_20ms_handle);
    qs_on_action_1s_handle_set(on_action_1s_handle);
}


void qs_action_data_process(void)
{
    qs_tft_protocol_data_process();
}